//
//  ViewController.m
//  city
//
//  Created by Apple on 2017/6/16.
//  Copyright © 2017年 闪电极送. All rights reserved.
//

#import "ViewController.h"
#import "DataBase.h"
#import "CityName.h"
#import "BMChineseSort.h"


@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *cityArray;
    NSMutableArray *dataArray;
    UILabel *addressLabel;
}
@property(nonatomic,copy)NSMutableArray *indexArray;
@property(nonatomic,copy)NSMutableArray *letterResultArr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self getCity];
    
    
    self.indexArray = [BMChineseSort IndexWithArray:dataArray Key:@"cityname"];
    self.letterResultArr = [BMChineseSort sortObjectArray:dataArray Key:@"cityname"];
    
    
    [self UIseting];
}


- (void)getCity
{
     cityArray = [[DataBase sharedDataBase] getAllCity];
  //模拟网络请求接收到的数组对象 city数组
    dataArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i = 0; i<[cityArray count]; i++) {
        CityName *p = [[CityName alloc] init];
        p.cityname = [cityArray objectAtIndex:i];
        p.number = i;
        [dataArray addObject:p];
    }

}
- (void)UIseting
{
    addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    addressLabel.font = [UIFont systemFontOfSize:20];
    addressLabel.textColor = [UIColor redColor];
    addressLabel.textAlignment = NSTextAlignmentLeft;
    
    [self.view addSubview:addressLabel];
    
    
    self.cityTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.cityTableview.delegate = self;
    self.cityTableview.dataSource = self;
    [self.view addSubview:self.cityTableview];
}
///section的titleHeader
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.indexArray objectAtIndex:section];
}
//section行数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.indexArray count];
}
//每组section个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[self.letterResultArr objectAtIndex:section] count];
}
//section右侧index数组
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.indexArray;
}
//点击右侧索引表项时调用 索引与section的对应关系
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    return index;
}
//返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
    }
    //获得对应的Person对象
    CityName *p = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.textLabel.text = p.cityname;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    CityName *p = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    addressLabel.text = p.cityname;
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
